//
//  TGLSqingJiaViewController.m
//  TuoGLaos
//
//  Created by 欧腾 on 2017/12/25.
//  Copyright © 2017年 outeng. All rights reserved.
//

#import "TGLSqingJiaViewController.h"
#import "TGLSqingJiaTableViewCell.h"
#import "TGLSHomeModel.h"
#import "TGLSqingJiaDetailViewController.h"
@interface TGLSqingJiaViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView*tableView;
@property(nonatomic,strong)TGLSKongView*backView;

@property(nonatomic,strong)NSMutableArray*qingJiaArray;
@property(nonatomic,strong)NSMutableArray*qingJiaArraySet; //数组应用数组
//加载第几页
@property(nonatomic,assign)NSInteger page;
@end

@implementation TGLSqingJiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //初始化
    [self createUI];
  
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    //请求数据
    if (kStringIsEmpty(UserToken)) {
        return;
    }
    self.page = 1;
    [self.qingJiaArraySet removeAllObjects];
    [self downHomeWorkList];
}
-(void)createUI{
    self.navigationItem.title = @"请假";
    
    self.backView= [[TGLSKongView alloc] initWithFrame:CGRectMake(kScreenWidth/2-60,kScreenHeight/2-60,120 , 120)];
    self.backView.hidden = YES;
    [self.view addSubview: self.backView];
    
    self.qingJiaArray = [[NSMutableArray alloc]init];
    self.qingJiaArraySet = [[NSMutableArray alloc] init];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
       self.tableView.backgroundColor = UIColorHex(0xF4F2F3);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"TGLSqingJiaTableViewCell" bundle:nil] forCellReuseIdentifier:@"TGLSqingJiaTableViewCell"];
    [self.view addSubview:self.tableView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self.qingJiaArraySet removeAllObjects];
        [self downHomeWorkList];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self downHomeWorkList];
    }];
}
-(void)downHomeWorkList{
    [[DDNetworkManagerDate makeUrlIndexmember_leave_list:UserToken school_id:[USER_DEFAULT objectForKey:@"school_id"] login_id:[USER_DEFAULT objectForKey:@"login_id"] num:@"" page:[NSString stringWithFormat:@"%ld",(long)self.page]] post_RequestFinshSuccess:^(id responseObject) {
//        NSLog(@"%@",responseObject);
        NSNumber *code = [responseObject objectForKey:@"code"];
         if([code integerValue] == 100) {
             self.qingJiaArray = (NSMutableArray *)[NSArray yy_modelArrayWithClass:[TGLSleave_listModel class] json:responseObject[@"list"]];
             for (int i = 0; i<self.qingJiaArray.count; i++) {
                 TGLSleave_listModel*model = self.qingJiaArray[i];
                 [self.qingJiaArraySet addObject:model];
             }
         }
        if ([code integerValue] == 102) {
            
            [USER_DEFAULT setObject:@"" forKey:@"token"];
            [USER_DEFAULT synchronize];
            [XHToast showCenterWithText:responseObject[@"msg"]];
            TGLSLoginViewController*login = [[TGLSLoginViewController alloc]init];
            
            [self.navigationController pushViewController:login animated:YES];
        }
        if (self.qingJiaArraySet.count == 0){
            self.backView.hidden = NO;
        }else{
            self.backView.hidden = YES;
        }
       
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    }failure:^(id errorObject) {
        
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.qingJiaArraySet.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TGLSqingJiaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TGLSqingJiaTableViewCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = UIColorHex(0xF4F2F3);
   
    [cell configWithModel:self.qingJiaArraySet[indexPath.row]];
    cell.selectionStyle = 0;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TGLSleave_listModel * model = self.qingJiaArraySet[indexPath.row];
    
    TGLSqingJiaDetailViewController*td = [[TGLSqingJiaDetailViewController alloc] init];
    td.leave_id = model.ids;
    td.status = model.status;
    [self.navigationController pushViewController:td animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
